package com.asset.dispoal.file.casedispose.service.command;

import com.asset.dispoal.business.bsCase.domain.db.entity.BsCaseDisposalFile;
import com.asset.dispoal.business.bsCase.domain.db.mapper.BsCaseMapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import tech.mhuang.pacebox.core.date.DateTimeUtil;
import tech.mhuang.pacebox.core.file.FileUtil;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;

import java.io.File;
import java.time.LocalDateTime;
import java.util.List;

@Component
public class FileCaseDisposeSaveFileCommand {

    @Autowired
    Environment environment;

    @Autowired
    BaseIdeable<String> ideable;

    @Autowired
    BsCaseMapper caseMapper;

    @SneakyThrows
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public synchronized void execute(String caseId, List<MultipartFile> fileList, String caseState, String fileType) {
        String userId = GlobalHeaderThreadLocal.get().getUserId();
        String headUrl = environment.getProperty("file.evidence.url");
        String caseRegisterFolder = environment.getProperty("file.caseRegister.url");
        LocalDateTime currentLocalDate = LocalDateTime.now();
        StringBuilder strBuilder = new StringBuilder();
        strBuilder.append(caseRegisterFolder)
                .append(File.separator)
                .append(currentLocalDate.getYear())
                .append(File.separator)
                .append(currentLocalDate.getMonthValue())
                .append(File.separator)
                .append(currentLocalDate.getDayOfMonth())
                .append(File.separator)
                .append(fileType)
                .append(File.separator);
        File checkDirFile = new File(headUrl + strBuilder);
        if (!checkDirFile.exists()) {
            FileUtil.createDirectory(checkDirFile, true);
        }
        for (MultipartFile file : fileList) {
            String originalName = file.getOriginalFilename();
            String replaceName = ideable.generateId() + originalName;
            file.transferTo(new File(headUrl + strBuilder + replaceName));
            BsCaseDisposalFile caseDisposalFile = new BsCaseDisposalFile();
            caseDisposalFile.setCaseId(caseId);
            caseDisposalFile.setId(ideable.generateId());
            caseDisposalFile.setReplaceName(replaceName);
            caseDisposalFile.setOriginalName(originalName);
            caseDisposalFile.setFilePath(strBuilder + replaceName);
            caseDisposalFile.setFileType(fileType);
            caseDisposalFile.setCurrentState(caseState);
            caseDisposalFile.setCreateUser(userId);
            caseDisposalFile.setOperateUser(userId);
            caseDisposalFile.setCreateTime(DateTimeUtil.localDateTimeToDate(currentLocalDate));
            caseDisposalFile.setOperateTime(DateTimeUtil.localDateTimeToDate(currentLocalDate));
            caseDisposalFile.setUploadWay("0");
            caseMapper.insertDisposeFile(caseDisposalFile);
        }
    }
}
